#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

class Computer{
public:
    Computer(const char * brand, float price)
    :_brand(new char[strlen(brand) + 1]())
    ,_price(price)
    {
        strcpy(_brand, brand);
        cout << "Computer(const char *,float)" << endl;
    }

    ~Computer(){
        if(_brand){
            delete [] _brand;
            _brand = nullptr;
        }
        cout << "~Computer" << endl;
    }
    //拷贝构造函数、赋值运算符函数也需要写出来
    //但是本案例就不再写了

    void * operator new(size_t size){
        cout << "operator new:" << size << endl;
        return malloc(size);
    }

    void operator delete(void * p){
        cout << "operator delete" << endl;
        free(p);
    }

    void print(){
        cout << _brand << "," << _price << endl;
    }

private:
    char * _brand;
    float _price;
};


void test(){
    cout << sizeof(Computer) << endl;
    Computer * pc = new Computer("Apple", 10000);
    //编译器会如何解析该函数
    //print(pc);
    pc->print();
    //编译器会如何解析该函数
    //print(&*pc);
    (*pc).print();
    delete pc;
}

int main()
{
    test();
    return 0;
}

